package com.rpframework.website.device.act.admin;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.rpframework.core.api.FileService;
import com.rpframework.utils.DateUtils;
import com.rpframework.utils.NumberUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.service.UserService;

/**
 * 用户表
 * @author zl
 * @date 2015年12月14日 下午4:04:01
 */
@Controller
@RequestMapping("/admin/userxg")
public class AdminUserxgAct extends AdminAct{
	@Resource UserService service;
	@Resource FileService fileService;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/list")
	public String list(@RequestParam(value="pager", required=false) Pager pager, 
			@RequestParam(value="state", required=false) Integer state, 
			Map<Object, Object> model){
		if(pager == null) {
			pager = new Pager();
		}
		if(NumberUtils.isValid(state)){
			pager.getSearchMap().put("state", state.toString());
			model.put("state", state);
		}
		pager = service.getPager(pager);
		model.put("pager", pager);
		return this.doPackageURI("user/list");
	}
	
	@RequestMapping("/{userId}/detail")
	public String detail(@PathVariable Integer userId,RedirectAttributes attr, Map<Object, Object> model,HttpSession session){
		User user = service.select(userId);
		Assert.notNull(user,"Not find userId: " + userId);
		model.put("user", user);
		
		return this.doPackageURI("user/detail");
	}
	@RequestMapping("/add")
	public String add(RedirectAttributes attr,Map<Object, Object> model){
		User user = service.select(1);
		model.put("user", user);
		return this.doPackageURI("userxg/edit");
	}
	@RequestMapping("/dosave")
	public String doSaveOrUpdate(@ModelAttribute User t,
			@RequestParam(value="coverPath", required=false) MultipartFile coverPath,
			HttpSession session,
			RedirectAttributes attr){
		if(coverPath!=null && coverPath.getSize() > 0) {
			try {
				String relativelyMaplinePath = "/device/interact/" + DateUtils.nowDate(DateUtils.YYYYMMDDHHMMSS) + NumberUtils.random() + "." + FilenameUtils.getExtension(coverPath.getOriginalFilename());
				fileService.saveFile(coverPath.getInputStream(), relativelyMaplinePath); 
				t.setHeadImg(relativelyMaplinePath);
			} catch (Exception e) {
				throw new IllegalArgumentException("文件上传失败，原因:" + e.getLocalizedMessage());
			}
		} else {
			
		}

		boolean flag = service.doSaveOrUpdate(t);
		if(flag){
			if(NumberUtils.isValid(t.getId())) {
				setInfoMsg("更新操作成功！", attr);
			} else {
				setInfoMsg("新增操作成功！", attr);
			}
		}else{
			setInfoMsg("操作失败！", attr);
		}
		return redirect("/admin/user/list");
	}
}

